package com.stud.business.stud.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.stud.business.stud.domain.Stud;
import com.stud.business.stud.service.StudService;
import com.stud.common.annotation.Log;
import com.stud.common.core.controller.BaseController;
import com.stud.common.core.domain.Result;
import com.stud.common.core.page.TableDataInfo;
import com.stud.common.enums.BusinessType;
import com.stud.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * studController
 *
 * @author Stud
 * @date 2023-07-27
 */
@Api(tags = "stud测试")
@RestController
@RequestMapping("/stud/stud")
@RequiredArgsConstructor
public class StudController extends BaseController {
    private final StudService studService;

    @ApiOperation("查询stud列表")
    @PreAuthorize("@ss.hasPermi('stud:stud:list')")
    @GetMapping("/list")
    public TableDataInfo<Stud> list(Stud stud) {
        startPage();
        Wrapper<Stud> wrapper = new QueryWrapper<>(stud);
        List<Stud> list = studService.list(wrapper);
        return getDataTable(list);
    }

    @ApiOperation("导出stud列表")
    @PreAuthorize("@ss.hasPermi('stud:stud:export')")
    @Log(title = "stud", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Stud stud) {
        Wrapper<Stud> wrapper = new QueryWrapper(stud);
        List<Stud> list = studService.list(wrapper);
        ExcelUtil<Stud> util = new ExcelUtil<>(Stud.class);
        util.exportExcel(response, list, "stud数据");
    }

    @ApiOperation("获取stud详细信息")
    @PreAuthorize("@ss.hasPermi('stud:stud:query')")
    @GetMapping(value = "/{id}")
    public Result<Stud> getInfo(@PathVariable("id") Long id) {
        return Result.ok(studService.getById(id));
    }

    @ApiOperation("新增stud")
    @PreAuthorize("@ss.hasPermi('stud:stud:add')")
    @Log(title = "stud", businessType = BusinessType.INSERT)
    @PostMapping
    public Result<Boolean> add(@Validated @RequestBody Stud stud) {
        return Result.ok(this.studService.save(stud));
    }

    @ApiOperation("修改stud")
    @PreAuthorize("@ss.hasPermi('stud:stud:edit')")
    @Log(title = "stud", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result<Boolean> edit(@Validated @RequestBody Stud stud) {
        return Result.ok(this.studService.update(stud));
    }

    @ApiOperation("删除stud")
    @PreAuthorize("@ss.hasPermi('stud:stud:remove')")
    @Log(title = "stud", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public Result<Boolean> remove(@PathVariable Long[] ids) {
        return Result.ok(this.studService.removeByIds(ids));
    }
}
